﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Interfejsi;
using MySql.Data.MySqlClient;
using OOAD_PROJEKAT;

namespace DAL
{
    partial class DAL
    {
        public class zahtjevDAO : IDaoCrud<zahtjevPolice>
        {

            protected MySqlCommand c;



            public long create(zahtjevPolice z)
            {
                try
                {

                    c = new MySqlCommand();
                    c.CommandText = "insert into zahtjevizapolicama values (@idzahtjeva, @idzaposlenika, @kolicina, @odobrena)";                                                                                            //                                           
                    c.Parameters.AddWithValue("@idzahtjeva", "");
                    c.Parameters.AddWithValue("@idzaposlenika", z.IdZaposlenika);
                    c.Parameters.AddWithValue("@kolicina", z.Kolicina);
                    c.Parameters.AddWithValue("@odobrena", z.Odobrena);
                    c.Connection = con;
                    c.ExecuteNonQuery();
                    return c.LastInsertedId;
                }
                catch (Exception e)
                {
                    global::System.Windows.MessageBox.Show(e.Message);
                    throw e;
                }
            }

            public List<zahtjevPolice> getAll()
            {
                c = new MySqlCommand("select * from zahtjevizapolicama", con);
                MySqlDataReader r = c.ExecuteReader();
                try
                {
                    
                    List<zahtjevPolice> police = new List<zahtjevPolice>();
                    while (r.Read())
                        police.Add(new zahtjevPolice(r.GetInt32("idzahtjeva"), r.GetInt32("idzaposlenika"), r.GetInt32("kolicina"), r.GetBoolean("odobrena")));

                   
                    return police;
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                    {
                        r.Close();
                    }
            }


            public zahtjevPolice read(zahtjevPolice z)
            {
                try
                {
                    string commandStr = "select * from zahtjevizapolicama where idzahtjeva=" + z.Id;
                    c = new MySqlCommand(commandStr, con);
                    MySqlDataReader r = c.ExecuteReader();
                    zahtjevPolice pr = null;
                    if (r.Read())
                        pr = new zahtjevPolice(r.GetInt32("idzahtjeva"), r.GetInt32("idzaposlenika"), r.GetInt32("kolicina"), r.GetBoolean("odobrena"));
                    r.Close();
                    return pr;
                }

                catch (Exception e)
                {
                    throw e;
                }
            }

            public zahtjevPolice update(zahtjevPolice z)
            {
                try
                {
                    String commandStr = "update zahtjevizapolicama set odobrena = ?c, ";
                    commandStr += "kolicina = ?b, odobrena = ?c ";
                    commandStr += "where idzahtjeva = ?d";

                    c = new MySqlCommand(commandStr, con);
                    c.Parameters.AddWithValue("?a", z.IdZaposlenika);
                    c.Parameters.AddWithValue("?b", z.Kolicina);
                    c.Parameters.AddWithValue("?c", z.Odobrena);
                    c.Parameters.AddWithValue("?d", z.Id);

                    c.ExecuteNonQuery();
                    return read(z);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public void delete(zahtjevPolice z)
            {
                try
                {
                    string commandStr = "DELETE FROM zahtjevizapolicama WHERE idzahtjeva=" + z.Id;
                    c = new MySqlCommand(commandStr, con);
                    c.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw e;
                }

            }

            public zahtjevPolice getById(int id)
            {                
                try
                {
                    zahtjevPolice z = new zahtjevPolice(id, 0, 0, false);
                    return read(z);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<zahtjevPolice> getByExample(string name, string value)
            {               
                try
                {
                    c = new MySqlCommand("select * from zahtjevizapolicama where " + name + "= " + value, con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<zahtjevPolice> police = new List<zahtjevPolice>();
                    while (r.Read())
                        police.Add(new zahtjevPolice(r.GetInt32("idzahtjeva"), r.GetInt32("idzaposlenika"), r.GetInt32("kolicina"), r.GetBoolean("odobrena")));

                    r.Close();
                    return police;
                }
                catch (Exception e)
                {
                    throw e;
                }
                 
            }           

        }
    }
}